package com.example.sparksqltest.demos.controller;

import com.example.sparksqltest.demos.dao.MovieRating;
import com.example.sparksqltest.demos.service.MovieDataService;
import org.apache.spark.sql.SparkSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@RestController
@RequestMapping("/api/movies")
public class MovieDataController {

    private static final Logger logger = LoggerFactory.getLogger(MovieDataController.class);

    private final MovieDataService movieDataService;
    private final SparkSession sparkSession;

    public MovieDataController(MovieDataService movieDataService, SparkSession sparkSession) {
        this.movieDataService = movieDataService;
        this.sparkSession = sparkSession;
    }

    @GetMapping("/ratings")
    public List<MovieRating> fetchMovieRatings() {
        logger.info("正在获取电影评分数据...");
        List<MovieRating> ratings = movieDataService.getMovieRatings(sparkSession);
        logger.info("成功获取到{}条电影评分数据。", ratings.size());
        return ratings;
    }
}
